package com.third.customer.site.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.third.customer.model.dao.param.CustomerGroupParam;
import com.third.customer.model.data.JsonResult;
import com.third.customer.model.data.UserType;
import com.third.customer.model.tables.CustomerGroup;
import com.third.customer.service.CustomerGroupService;
import com.third.customer.service.data.CustomerGroupReq;
import com.third.customer.service.utils.PageResult;
import com.third.customer.site.UserInfo;
import com.third.customer.site.controller.resp.CustomerGroupResp;
import com.third.customer.site.support.ThirdAuth;
import com.third.customer.site.support.ThirdUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by hehuaichun on 2020/5/22.
 */
@RestController
@RequestMapping("/customer-groups")
public class CustomerGroupCtrl {

    @Autowired
    private CustomerGroupService customerGroupService;

    @GetMapping
    @ThirdAuth(UserType.ORDINARY)
    public JsonResult list(@ThirdUser UserInfo userInfo,
                           @RequestParam(required = false) String name,
                           @RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit) {
        CustomerGroupParam param = new CustomerGroupParam();
        param.setName(name);
        param.setLimit(limit);
        param.setPage(page);
        param.setCompanyId(userInfo.getCompanyId());
        Page<CustomerGroup> groupPage = customerGroupService.list(param);
        List<CustomerGroupResp> respList = groupPage.getRecords().stream().map(CustomerGroupResp::convert).collect(Collectors.toList());
        return JsonResult.ok(PageResult.build(groupPage, respList));
    }

    @GetMapping("/{id}")
    @ThirdAuth(UserType.ORDINARY)
    public JsonResult getById(@ThirdUser UserInfo userInfo,
                              @PathVariable Long id) {
        return JsonResult.ok(CustomerGroupResp.convert(customerGroupService.getById(userInfo.getCompanyId(), id)));
    }

    @PostMapping
    @ThirdAuth(UserType.ORDINARY)
    public JsonResult create(@ThirdUser UserInfo userInfo,
                             @Valid CustomerGroupReq req) {
        req.validateForCreate();
        return JsonResult.ok(CustomerGroupResp.convert(customerGroupService.create(userInfo.getCompanyId(), req)));
    }

    @PostMapping("/{id}/modify")
    @ThirdAuth(UserType.ORDINARY)
    public JsonResult modify(@ThirdUser UserInfo userInfo,
                             @PathVariable Long id,
                             @Valid CustomerGroupReq req) {
        req.validateForModify();
        return JsonResult.ok(CustomerGroupResp.convert(customerGroupService.modify(userInfo.getCompanyId(), id, req)));
    }

    @PostMapping("/{id}/delete")
    @ThirdAuth(UserType.ORDINARY)
    public JsonResult delete(@ThirdUser UserInfo userInfo,
                             @PathVariable Long id) {
        return JsonResult.ok(customerGroupService.delete(userInfo.getCompanyId(), id));
    }
}
